package com.lun.swordtowardoffer2.c04;

import com.lun.util.SinglyLinkedList.ListNode;

public class RemoveNthFromEnd {
	
	public ListNode removeNthFromEnd(ListNode head, int n) {
		ListNode dummy = new ListNode(-1);
		dummy.next = head;
		
		ListNode p1 = dummy, p2 = dummy;
		
		while(n-- > 0) {
			p2 = p2.next;
		}
		
		while(p2.next != null) {
			p1 = p1.next;
			p2 = p2.next;
		}
		
		p1.next = p1.next.next;
		
		return dummy.next;
	}
}
